<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>魏远商城</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="css/sm.css">
		<link href="css/accound.css" rel="stylesheet">
		<link href="css/jiaoben.css" />
	</head>

	<body>
		<div class="head">
			<img class="mui-action-back" src="img/back-le.png" alt="">
			<p>结算</p>
		</div>
		<div class="fixed">
			<div class="f-all">
				<p>
					<span>共<span class="alljian"></span>件</span>&nbsp; 合计：
					<span>¥<span class="allprice"></span></span>
				</p>
			</div>
			<div class="buttom">
				提交订单
			</div>
		</div>
		<div class="content">
			<div class="address">
				<p style="margin-bottom: 15px;">
					<span>收货人：<span class="name"></span></span>
					<span class="tel"></span>
				</p>
				<div>
					<div>
						<img src="img/address.png">
					</div>
					<div>
						<p>收货地址：<span class="area"></span></p>
						<p class="details"></p>
					</div>
					<div>
						<img src="img/mine-jin.png">
					</div>
				</div>
				<!--<p>（收货不便时，可选择免费代收费服务）</p>-->
			</div>
			<div class="content-goods">
				<!-- <div class="goods">
                <div><span class="goods-pic"></span></div>
                <div>
                    <p class="name">车陂、长裤</p>
                    <p>数量：<span>1</span>&nbsp&nbsp颜色：<span>黄色</span>&nbsp&nbsp尺码：<span>38</span></p>
                </div>
                <div>
                    <p class="price">¥<span>139</span></p>
                    <p class="how">×<span>1</span></p>
                </div>
            </div>-->

			</div>
			<div class="type">
				<div class="come">
					<p class="selectway">选择配送方式</p>
					<div class="noneor">选择</div>
					<div class="allway">

						<!--<p>韵达物流</p>
						<p>ems</p>
						<p>邮政小包</p>
						<p>中通物流</p>
						<p>韵达物流</p>
						<p>ems</p>
						<p>邮政小包</p>
						<p>中通物流</p>
						<p>韵达物流</p>
						<p>ems</p>
						<p>邮政小包</p>-->
					</div>
					<!--<p class="yunall"><span class="yuntype"></span>¥<span class="yunprice"></span></p>-->
					<!--<div class="menu">
						<div class="bt-name" id="year_x">年份</div>
						<div class="sanjiao"></div>
						<div class="new-sub" id="year">
							<ul>
								<li>全部</li>
								
							</ul>
							<div class="tiggle"></div>
							<div class="innertiggle"></div>
						</div>
					</div>-->
				</div>
				<!--<div class="xian">
					<p>运费险</p>
					<p>无</p>
				</div>-->
				<!--<div class="tell">
					<p>给卖家留言</p><input class="leaveWord" type="text" placeholder="选填：对本次交易的说明">
				</div>-->
				<!--<div class="all">
                <p>
                    <span>共<span>1</span>件</span>&nbsp;
                    合计：<span>¥<span>180.00</span></span>
                </p>
            </div>-->
			</div>
			<div class="tell">
				<p>给卖家留言</p>
				<div>
					<textarea maxlength="100" class="leaveWord" type="text" placeholder="选填：对本次交易的说明" name="" rows="" cols=""></textarea>
				</div>
				<!--<input class="leaveWord" type="text" placeholder="选填：对本次交易的说明">-->
			</div>
			<div class="coupon">
				<div>
					<span class="clicuse"><span></span></span>
					<p>是否使用优惠券</p>
				</div>
				<div class="inputcou">
					<div><input id="counnum" type="text" placeholder="请输入优惠券号码"></div>
					<div class="suretouse">确定</div>
				</div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/jquery-3.1.1.min.js"></script>
		<script src="js/url.js"></script>
		<script type="text/javascript">
			showsure();
			$('.address').on('tap', function() {
				/* href('my-address.html');*/
				mui.openWindow({
					url: "my-address.html",
					id: "my-address",
				})
			});

			/*	function href(a) {
				
				            mui.openWindow({
				                url: a,
				                show: {
				                    aniShow: 'pop-in'
				                }
				            })
				        }*/

			/*点击使用优惠券*/
			$('.clicuse').on('tap', function() {
				//					alert(111);
				console.log($(this).children('span').css('background-color'));
				if($(this).children('span').css('background-color') == 'rgb(255, 255, 255)') {
					$(this).children('span').css('background-color', '#e9af28');
					$('.inputcou').css('display', 'block');
				} else if($(this).children('span').css('background-color') == 'rgb(233, 175, 40)') {
					$(this).children('span').css('background-color', 'rgb(255, 255, 255)');
					$('.inputcou').css('display', 'none');
				}
			})

			/*使用优惠券接口*/
			$('.suretouse').on('tap', function() {
				if($('#counnum').val() == '') {
					mui.toast('请输入优惠券号码');
				} else {
					usecoun();
				}

			});

			function usecoun() {
				$.ajax({
						url: ur + '/mobilemember/shoppingcart/getByCoupons',
						type: 'POST',
						headers: {
							"X-Token": localStorage.token
						},
						dataType: 'json',
						data: {
							"couponsNo": $('#counnum').val(),
							"totalPrice": localStorage.allprice
						},
					})
					.done(function(data) {
						if(data.result.meta.code == 202) {
							mui.toast('优惠券已过期或者已被使用');
						} else if(data.result.meta.code == 201) {
							mui.toast('优惠券不存在');
						} else if(data.result.meta.code == 200) {
							mui.toast('优惠券使用成功');
							console.log(data);
							$('.suretouse').off('tap');
							$('.suretouse').css('background-color', '#929292');
							//						var ppprice = String(data.newPrice);
							$('.allprice').text(data.result.data.newPrice);
							localStorage.allprice = $('.allprice').text();
							localStorage.youhuiquan = $('#counnum').val();
							//						    alert(localStorage.youhuiquan);
						}
					})
					.fail(function() {
						mui.toast('系统错误');
					})
			}

			/*点击选择物流*/
			$('.noneor').on('tap', function() {
				$('.allway').toggle();
			})

			localStorage.sycdyf = 0;
			$('.allway').on('tap', 'p', function() {

				$('.allway').css('display', 'none');
				$('.noneor').text($(this).text());
				$(this).children('.yunpri').text();

				allpricell2 = Number(localStorage.allprice) + Number($(this).find('.yunpri').text()) - Number(localStorage.sycdyf);

				localStorage.sycdyf = $(this).find('.yunpri').text();
				$('.allprice').text(allpricell2.toFixed(2));
				localStorage.allprice = $('.allprice').text();

				localStorage.yunid = $(this).attr('id');
				localStorage.yunprice = $(this).children('.yunpri').text();
				//			     	localStorage.allprice = $('.allprice').text();
				//			     	alert($(this).attr('id'));
				//			     	alert($(this).find('.yunpri').text());

			})

			/*获取默认收货地址*/
			window.addEventListener('upaddress', function(event) {
				showaddress();
				//		           alert(111);
			});
			
			showaddress();
			function showaddress() {
				$.ajax({
						url: ur + '/mobilemember/frontmember/getDefauReceiAddr',
						type: 'POST',
						headers: {
							"X-Token": localStorage.token
						},
						dataType: 'json',
						data: ''
					})
					.done(function(data) {
						console.log(data);
						if(data.result.meta.code == 200) {
							$('.name').text(data.result.data.reciver);
							$('.tel').text(data.result.data.phone);
							$('.area').text(data.result.data.area);
							$('.details').text(data.result.data.detail);
						} else if(data.result.meta.code == 201) {

							mui.alert('您还没有默认地址，前往设置默认地址', '魏远商城', function() {
								mui.openWindow({
									url: "my-address.html",
									id: "my-address",
								})
							});
						} else if(data.result.meta.code == 202) {
							mui.alert('您还没有收货地址，前往新增地址', '魏远商城', function() {
								mui.openWindow({
									url: "my-address.html",
									id: "my-address",
								})
							});
						}

					})
					.fail(function() {
						//						mui.toast('系统错误');
					})
			}




			var allpricell = 0;
			/*获取确认购买商品展示*/
//			showsure();

			function showsure() {
				
				$.ajax({
						url: ur + '/mobilemember/shoppingcart/confirmationOfOrder',
						type: 'POST',
						headers: {
							"X-Token": localStorage.token
						},
						dataType: 'json',
						data: {
							"cskuIds": localStorage.skid,
							"ccounts": localStorage.how,
							"cprices": localStorage.pricejh,
							"cproductIds": localStorage.proid,
						},
					})
					.done(function(data){
						if(data.result.meta.code == 200) {
							console.log(data);
							var price = 0;
							var how = 0;
							var scount = '';
							var sname = '';
							var sdan = '';
							var skid7 = '';
							var startall = '';
							
							var jdcount = '';
							var jdname = '';
							var jdprice = '';
							var jdpskuids = '';
							for(var i = 0; i < data.result.data.length; i++) {
								var b =  data.result.data[i].imgsrc;
								console.log(b.indexOf(',') + '逗号索引值');
								var m = b.indexOf(',');
								if(m != '-1') {
									var mainImg = b.substring(0, m);
								} else {
									mainImg = b;
								}

								var div = $('<div class="goods"> ' +
									'<div><span class="goods-pic" style="background:url(' + picur + mainImg+ ');background-position: center;background-repeat: no-repeat;background-size: cover;"></span></div> ' +
									'<div class="infirma' + i + ' infirma"> <div class="shortname">' + data.result.data[i].shortName + '</div> <div class="stype">' + data.result.data[i].size + '</div>  </div> ' +
									'<div> <p class="price">¥<span>' + data.result.data[i].price + '</span></p> <p class="how">×<span>' + data.result.data[i].number + '</span></p> </div> </div>')

								$('.content-goods').append(div);
								how = how + Number(data.result.data[i].number);
								price = price + data.result.data[i].price * data.result.data[i].number;
								console.log(price);

                                if(data.result.data[i].jdProductId!=null && data.result.data[i].jdProductId!=''){
                                	alert(data.result.data[i].jdProductId);
                                	jdcount = jdcount + data.result.data[i].number + ',';
                                	jdname = jdname + data.result.data[i].shortName + ',';
                                	jdprice = jdprice + data.result.data[i].price + ',';
                                	jdpskuids = jdpskuids + data.result.data[i].skuId + ',';
                                }else{
                                	scount = scount + data.result.data[i].number + ',';
								    sname = sname + data.result.data[i].shortName + ',';
								    sdan = sdan + data.result.data[i].price + ',';
								    skid7 = skid7 + data.result.data[i].skuId + ',';
                                }
                                
								
//								console.log(jdcount);
//								console.log(jdname);
//								console.log(jdprice);
//								console.log(jdpskuids);
//								
//								/*sskuid = sskuid+data.result.data[i].skuProductVO.skuId+',';*/
//
//								console.log(scount);
//								console.log(sname);
//								console.log(sdan);
								if(data.result.data.propVOs) {
									console.log(data.result.data[0].propVOs.length);
									var starts = '';
									for(var y = 0; y < data.result.data[i].propVOs.length; y++) {
										var singal = data.result.data[i].propVOs[y].proName + ':' + data.result.data[i].propVOs[y].proValue + ';';
										starts = starts + singal;
										//								console.log(starts + '二次for循环便利');
										var divp = $('<span>' + singal + '</span>')
										$('.infirma' + i + '').append(divp);
									}
								}
								startall = startall + starts + ',';
								console.log(startall);
								sss = startall.substring(0, startall.length - 1);
								//							console.log(sss);
							}
						}
						
						localStorage.jdcount = jdcount.substring(0, jdcount.length - 1);
						localStorage.jdname = jdname.substring(0, jdname.length - 1);
						localStorage.jdprice = jdprice.substring(0, jdprice.length - 1);
						localStorage.jdpskuids = jdpskuids.substring(0, jdpskuids.length - 1);
						console.log(localStorage.jdcount);
						console.log(localStorage.jdname);
						console.log(localStorage.jdprice);
						console.log(localStorage.jdpskuids);
						
						
						localStorage.nature = startall.substring(0, startall.length - 1);
						localStorage.scount = scount.substring(0, scount.length - 1);						
						localStorage.sname = sname.substring(0, sname.length - 1);
						localStorage.sdan = sdan.substring(0, sdan.length - 1);
						localStorage.skid7 = skid7.substring(0, skid7.length - 1);
                        console.log(localStorage.scount);
						console.log(localStorage.sname);
						console.log(localStorage.sdan);
						console.log(localStorage.skid7);
                        

						allpricell = Number(allpricell) + Number(price);

						$('.alljian').text(data.result.data.length);

						$('.allprice').text(allpricell.toFixed(2));
						localStorage.allprice = allpricell;

						/*获取运费接口*/
						getyun();

						function getyun() {

							if(localStorage.proid.indexOf(",") > 0) {
								proidan = localStorage.proid.substring(0, localStorage.proid.indexOf(","));
							} else {
								proidan = localStorage.proid;
							}
							/*   proidan = localStorage.proid.substring(0,localStorage.proid.indexOf(","));*/
							console.log(localStorage.proid + '>>>>>>>>>>>>>>');
							console.log(localStorage.proid.indexOf(",") + '>>>>>>>>>>>>>>');
							console.log(localStorage.proid.substring(0, localStorage.proid.indexOf(",")) + '>>>>>>>>>>>>>>');
							$.ajax({
									url: ur + '/mobile/freighttemplate/getfreightprice',
									type: 'POST',
									headers: {
										"X-Token": localStorage.token
									},
									dataType: 'json',
									data: {
										"area": $('.area').text(),
										"conunts": localStorage.how,
										"productIds": localStorage.proid,
									},
								})
								.done(function(data) {
									console.log(data);

									if(data.result.meta.code == 200) {

										console.log(data.result.data.length);

										for(var p = 0; p < data.result.data.length; p++) {
											console.log("1此00000000000000000");
											var divp = ('<p id="' + data.result.data[p].expressId + '">' + data.result.data[p].expressName + '&nbsp;&nbsp;&nbsp;<span>¥<span class="yunpri">' + data.result.data[p].freightPrice + '</span></span></p>');

											$('.allway').append(divp);
										}
										//										$('.yuntype').text(data.result.data[1].expressName);
										//										$('.yunprice').text(data.result.data[1].freightPrice);
										//										allpricell = Number(allpricell) + data.result.data[1].freightPrice;
										//										$('.allprice').text(allpricell.toFixed(2));
										//										localStorage.yunid = data.result.data[1].expressId;
										//									    localStorage.yunprice = data.result.data[1].freightPrice;
									} else if(data.result.meta.code == 202 || data.result.meta.code == 201) {
										$('.noneor').text('此地区无配送方式');
									}
								})
						}
					})
			}

			$('.buttom').on('tap', function() {
				if($('.noneor').text() == '选择') {
					mui.toast('请选择配送方式');
				} else if($('.noneor').text() == '此地区无配送方式 ') {
					mui.toast('此物品本地区无货,暂时不能购买');
				} else {
                    localStorage.enterP = 
					creatOrder();
					localStorage.allpriceall = $('.allprice').text();
					localStorage.alltimes = $('.alljian').text();
				}
				//				alert(666);
				//				alert(isbuy);                
			})

			/*创建订单*/
			function creatOrder() {
//				               alert(localStorage.allprice);
				console.log($('.area').text());
				console.log($('.details').text());
				console.log(localStorage.yunid);
				console.log(localStorage.yunprice);
				console.log($('.leaveWord').val());
				console.log(allpricell);
				console.log($('.tel').text());
				console.log(localStorage.nature);
				console.log(localStorage.scount);
				console.log(localStorage.sname);
				console.log(localStorage.sdan);
				console.log(localStorage.skid);
				console.log(localStorage.token);
				
				var addressArea = $('.details').text().split('');
				if(addressArea.length==3){
					var sheng7 = addressArea[0];
					var shi7 = addressArea[1];
					var qu7 = addressArea[2];
					var jie7 = '';
					
				}else if(addressArea.length==4){
					var sheng7 = addressArea[0];
					var shi7 = addressArea[1];
					var qu7 = addressArea[2];
					var jie7 = addressArea[3];
				}
				$.ajax({
						url: ur + '/mobilemember/frontOrder/createOrder',
						type: 'POST',
						headers: {
							"X-Token": localStorage.token
						},
						dataType: 'json',
						data: {
							"orderVO.city":shi7,
							"orderVO.province":sheng7,
							"orderVO.country":qu7,
							"orderVO.town":jie7,
							"jdPcounts":localStorage.jdcount,
							"jdPnames":localStorage.jdname,
							"jdPprice":localStorage.jdprice,
							"jdPskuids":localStorage.jdpskuids,
							"enterProductIds":localStorage.enterP,
							"orderVO.area": $('.area').text(),
							"orderVO.detail": $('.details').text(),
							"orderVO.expressId": localStorage.yunid,
							"orderVO.freight": localStorage.yunprice,
							"orderVO.leaveWord": $('.leaveWord').val(),
							"orderVO.orderPay": localStorage.allprice,
							"orderVO.phone": $('.tel').text(),
							"orderVO.reciver": $('.name').text(),
							"orderVO.remark": '',
							"pcounts": localStorage.scount,
							"pnames": localStorage.sname,
							"pprice": localStorage.sdan,
							"pskuids": localStorage.skid7,
							"type": 2,
							"comSkuIds": localStorage.zhproid
						},
					})
					.done(function(data) {
						console.log(data);
						if(data.result.meta.code == 200) {
							mui.toast('创建订单成功');
							localStorage.sureorder = data.result.data.orderNum;
							localStorage.sureorderid = data.result.data.orderId;
							
							delectgoods();
						}else if(data.result.meta.code == 202){
							mui.toast(data.result.meta.message);
						}
					})
					.fail(function(data){
						alert(JSON.stringify(data));
						console.log(JSON.stringify(data))
						mui.toast('系统错误');
					})
			}


			/*创建订单删除购物车和优惠券报废*/
			function delectgoods() {
				$.ajax({
					url: ur + '/mobilemember/shoppingcart/deleteshoppingcartByOrder',
					type: 'POST',
					headers: {
						"X-Token": localStorage.token
					},
					dataType: 'json',
					data: {
						"couponsno": localStorage.youhuiquan,
						"ordercouId": localStorage.sureorderid,
					},
					success: function(data) {
						console.log(data);
						mui.toast('创建订单成功');
						href('sure.html');
					},
					error: function(data) {
						mui.toast('系统错误');
					}
				})
			}


			function href(a) {
				mui.openWindow({
					url: a,
					show: {
						aniShow: 'pop-in'
					}
				})
			}
		</script>
	</body>

</html>